﻿CREATE FUNCTION [Mapping].[GetRuntimeViewHash]
(
  @groupId int
)
RETURNS INT
AS
BEGIN
  DECLARE @str nvarchar(500)

  ;WITH KeyColumns AS (
    SELECT DISTINCT KeyColumn
      FROM Mapping.SourceKey
      WHERE MappingItem IN (SELECT Id FROM Mapping.MappingItem WHERE [Group] = @groupId) 
  )
  SELECT @str = IIF(@str IS NULL, '', @str + ', ') + 
      KeyColumn
    FROM KeyColumns

  RETURN IIF(@str IS NULL, NULL, CHECKSUM(@str))
END
